package com.woniu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.domain.Resume;
import com.woniu.domain.User;
import com.woniu.dto.Result;
import com.woniu.dto.StatusCode;
import com.woniu.service.ResumeService;
import com.woniu.service.UserService;
import com.woniu.vo.PageVo;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.domain.Position;
import com.woniu.domain.Resume;
import com.woniu.dto.Result;
import com.woniu.dto.StatusCode;
import com.woniu.service.ResumeService;
import com.woniu.vo.PositionVo;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyy
 * @since 2021-03-08
 */
@RestController
@RequestMapping("/resume")
public class ResumeController {

    @Resource
    private ResumeService resumeService;
    @Resource
    private UserService userService;

    @PutMapping("sendHotResume")
    public Result sendHotResume(@RequestBody Resume resume){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>();
        userQueryWrapper.eq("id",resume.getUid());
        User userDB = userService.getOne(userQueryWrapper);

        if(!ObjectUtils.isEmpty(userDB)&&"vip".equals(userDB.getVip())){
            QueryWrapper<Resume> updateWrapper = new QueryWrapper<>();
            updateWrapper.eq("resume_id",resume.getResumeId());
            resume.setHot("平台推送");
            resumeService.update(resume, updateWrapper);
            return new Result(true, StatusCode.OK,"热门简历设置成功");
        }
        return new Result(false, StatusCode.ERROR,"该用户不是VIP用户,平台无法推送简历");
    }

    @GetMapping("getHotResumes")
    public Result getHotResumes(){
        List<Resume> positionAndCompany = resumeService.getHotPositions();
        return new Result(true, StatusCode.OK,"查询热门公司职位成功",positionAndCompany);
    }

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("findall/{uid}")
    public Result<Resume> findall(@PathVariable Integer uid) {
        System.out.println(uid);
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        Resume resume = resumeService.getOne(wrapper);

//        Object visitCount = stringRedisTemplate.opsForHash().get("resume:" + uid, "visit:count");
//        //如果没有值，前往数据库中查询
//        if (!ObjectUtils.isEmpty(visitCount)) {
//            resume.setVisitCount((Integer) visitCount);
//        }
        if(resume==null) {
        return new Result<>(false, StatusCode.OK, "查询失败", resume);
    }else
        {
        return new Result<>(true, StatusCode.OK, "查询成功", resume);
    }

}

////查看简历修改访问数方法
//public Integer updateVisitById(Integer uid){
//    //先去redis中查询
//    Object visitCount = stringRedisTemplate.opsForHash().get("resume:" + uid, "visit:count");
//    Integer updateVisitCount=null;
//    //如果没有值，前往数据库中查询
//    if (ObjectUtils.isEmpty(visitCount)) {
//        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
//        wrapper.eq("uid", uid);
//        Resume resume = resumeService.getOne(wrapper);
//        updateVisitCount=resume.getVisitCount()+1;
//        stringRedisTemplate.opsForHash().put("resume:"+uid,"visit:count",updateVisitCount.toString());
//        return updateVisitCount;
//    }
//    updateVisitCount=Integer.parseInt(visitCount.toString())+1;
//    stringRedisTemplate.opsForHash().put("resume:"+uid,"visit:count",updateVisitCount.toString());
//    return updateVisitCount;
//}









    @GetMapping("finduserresume/{uid}")
    public Result<Resume> finduserresume(@PathVariable Integer uid) {
        System.out.println(uid+"123");
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        Resume resume = resumeService.getOne(wrapper);
        if(resume!=null){
            return new Result<>(false, StatusCode.OK, "查询成功",1);
        }
        return new Result<>(true, StatusCode.OK, "查询失败",2);
    }

    @GetMapping("findone/{uid}")
    public Result<Resume> findone(@PathVariable Integer uid) {
        System.out.println(uid);
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        List<Resume> resume = resumeService.list(wrapper);
        return new Result<>(true, StatusCode.OK, "查询成功", resume);
    }


    @GetMapping("updataone/{rid}")
    public Result<Resume> updataone(@PathVariable Integer rid) {
        System.out.println(rid);
        UpdateWrapper<Resume> wrapper = new UpdateWrapper();
        wrapper.eq("resume_id", rid);
        Resume resume = new Resume();
        resumeService.update(resume, wrapper);
        return new Result<>(true, StatusCode.OK, "更新成功");
    }

    //查询热门职位
    @GetMapping("findallposition")
    public Result<PositionVo> findposition() {
        List<PositionVo> list = resumeService.findallPosition();
        System.out.println(list);
        return new Result<>(true, StatusCode.OK, "查询成功", list);

    }
    //搜索查询的热门职位
    @GetMapping("findposition/{tname}")
    public Result<Position> findposition(@PathVariable String tname){
        System.out.println(tname);
        List<PositionVo> list = resumeService.findPosition(tname);
        return new Result<>(true, StatusCode.OK, "查询成功", list);

    }


    @PostMapping("updateresume")
    public Result<Resume> updateresume(@RequestBody Resume resume){
        UpdateWrapper<Resume> wrapper = new UpdateWrapper();
        wrapper.eq("uid",resume.getUid());
        resumeService.update(resume, wrapper);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }


    @PostMapping("addresumes")
    public Result<Resume> addresumes(@RequestBody Resume resume){
        resume.setHot("非热点");
        resume.setVisitCount(0);
        stringRedisTemplate.opsForHash().put("resume:"+resume.getUid(), "visit:count", resume.getVisitCount().toString());
       resumeService.save(resume);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    @GetMapping("findOne/{id}")
    public Result<Resume> findOne(@PathVariable Integer id) {
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        wrapper.eq("resume_id", id);
        Resume resume = resumeService.getOne(wrapper);
        int visitCount;
        Object o = stringRedisTemplate.opsForHash().get("position:", "visit:position");
        //修改访问数
        if (ObjectUtils.isEmpty(o)) {
            visitCount = resume.getVisitCount()+1;
        }else {
            visitCount=Integer.parseInt(o.toString())+1;
        }
        stringRedisTemplate.opsForHash().put("position:"+id,
                "visit:position",Integer.toString(visitCount));
        return new Result<>(true, StatusCode.OK, "查询成功", resume);
    }
    
}